/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mytv.service;

import com.mytv.dao.Dao;
import com.mytv.entity.Config;
import java.io.Serializable;
import javax.annotation.Resource;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sirius
 */
@Service
public class ConfigService {
    
    public static final String PATH = "PATH";
    public static final String PLAYER = "PLAYER";
    
    @Resource
    private Dao<Config, Serializable> dao;

    /**
     * Constructor of config service
     */
    public ConfigService() {
    }
    
    /**
     * Save a new config
     * @param config 
     */
    public void save(Config config) {
        config.setCode(config.getCode().trim());
        config.setValue(config.getValue().trim());
        dao.save(config);
    }
    
    /**
     * Get if exist config with specified code
     * @return <code>true</code>: Config exists<br/><code>false</code>: Otherwise
     */
    public boolean isExistUserByNickName(String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Config.class);
        criteria.add(Restrictions.eq("code", code));
        return dao.count(criteria) > 0;
    }
    
    /**
     * Get config by nickname and password
     * @param code Code of config
     * @return The config with code specified
     */
    public Config getConfig(String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Config.class);
        criteria.add(Restrictions.eq("code", code));
        return dao.getUnique(criteria);
    }
    
    /**
     * Update existing config
     * @param config Config to update
     */
    public void updateConfig(Config config) {
        dao.update(config);
    }
    
}